<template>
  <div class="add-area">
    <div class="text">
      <el-icon><Tickets /></el-icon><span>数据列表</span>
    </div>
    <el-button type="primary" @click="add(null)"> 添加 </el-button>
  </div>
  <el-table
    :data="menuList"
    border
    table-layout="auto"
    cell-class-name="align-c"
  >
    <el-table-column label="编号" prop="id" />
    <el-table-column label="菜单名称" prop="title" />
    <el-table-column label="菜单级数" prop="level">
      <template #default="{ row }">
        <span>{{ row.level === 0 ? "一级" : "二级" }}</span>
      </template>
    </el-table-column>
    <el-table-column label="前端名称" prop="name" />
    <el-table-column label="前端图标" prop="icon">
      <template #default="{ row }">
        <span>{{ row.icon }}</span>
      </template>
    </el-table-column>
    <el-table-column label="是否显示" prop="hidden">
      <template #default="{ row }">
        <el-switch v-model="row.hidden" :active-value="0" :inactive-value="1" />
      </template>
    </el-table-column>
    <el-table-column label="排序" prop="sort" />
    <el-table-column label="设置" prop="id">
      <template #default="{ row }">
        <el-button link type="primary" @click="check(row.id)"
          >查看下级</el-button
        >
      </template>
    </el-table-column>
    <el-table-column label="操作" prop="id">
      <template #default="{ row }">
        <el-button link type="primary" @click="add(row.id)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>

  <ElConfigProvider :locale="zhCn">
    <div class="page">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="queryMenuList"
        @current-change="queryMenuList"
      />
    </div>
  </ElConfigProvider>
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import { getMenuList } from "./api/menu";
import { Tickets } from "@element-plus/icons-vue";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { ElMessage, ElConfigProvider } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();
//是否显示
// const switchStatue = (row: IMenuModel) => {
//   switchMenuStatusAPI(row.id!, row.hidden)
//     .then((res) => {
//       if (res.code === 200) {
//         ElMessage.success("修改成功！");
//       } else {
//         row.hidden = row.hidden === 1 ? 0 : 1;
//       }
//     })
//     .catch(() => {
//       row.hidden = row.hidden === 1 ? 0 : 1;
//     });
// };

const queryParams = reactive<IMenuListParams>({
  parentId: 0,
  pageNum: 1,
  pageSize: 10,
});
const total = ref(0);
const check = (id: number) => {
  queryParams.parentId = id;
  queryMenuList();
};
const add = (id: number | null) => {
  if (!id) {
    router.push("/ums/addMenu");
  } else {
    router.push(`/ums/addMenu?id=${id}`);
  }
};

const menuList = ref<IMenuModel[]>([]);
const queryMenuListAPI = () => {
  getMenuList(queryParams).then((res) => {
    // console.log("数据:", res.data);
    if (res.code === 200) {
      menuList.value = res.data.list;
      total.value = res.data.total;
    }
  });
};
const queryMenuList = () => {
  queryMenuListAPI();
};

queryMenuListAPI();
</script>
<style lang="less" scoped>
.el-table .cell {
  text-align: center;
}
:deep(.align-c) {
  .cell {
    text-align: center !important;
  }
}
.add-area {
  display: flex;
  justify-content: space-between;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 0 4px #ccc;
  border-radius: 5px;
}
.page {
  padding: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
